/**
 *  This class is the main class of the "World of Zuul" application. 
 *  "World of Zuul" is a very simple, text based adventure game.  Users 
 *  can walk around some scenery. That's all. It should really be extended 
 *  to make it more interesting!
 * 
 *  To play this game, create an instance of this class and call the "play"
 *  method.
 * 
 *  This main class creates and initialises all the others: it creates all
 *  rooms, creates the parser and starts the game.  It also evaluates and
 *  executes the commands that the parser returns.
 * 
 * @author  Michael K?lling and David J. Barnes
 * @version 2016.02.29
 */

public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private Player player;
    private int overtime = 15;
    private boolean havetirebal = false;
    private boolean gameover = false;
        
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        parser = new Parser();
        player = new Player();
    }

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
        Room outside, room1, room2, room3, room4, room5, room6, room7, room8,room9;
      
        // create the rooms
        outside = new Room("中路");
        room1 = new Room("香蕉道");
        room2 = new Room("侧道");
        room3 = new Room("下水道");
        room4 = new Room("马棚");
        room5 = new Room("喷泉",new Thing("炸弹"));
        room6 = new Room("花园");
        room7 = new Room("书房");
        room8 = new Room("墓地",new Thing("拆弹器"));
        room9 = new Room("阳台");
        
        // initialise room exits
        //outside.setExits(null, theater, lab, pub);
        outside.setExit("east", room1);
        room1.setExit("west",outside);
        outside.setExit("west", room2);
        room2.setExit("east", outside);
        outside.setExit("south", room3);
        room3.setExit("north", outside);
        outside.setExit("north", room4);
        room4.setExit("south", outside);
        room1.setExit("east", room5);
        room5.setExit("west", room1);
        room5.setExit("north", room6);
        room6.setExit("south", room5);
        room6.setExit("west", room7);
        room7.setExit("east", room6);
        room7.setExit("west", room8);
        room8.setExit("east", room7);
        room4.setExit("north", room8);
        room8.setExit("south", room7);
        room8.setExit("west", room9);
        room9.setExit("east", room8);
        room2.setExit("north", room9);
        room9.setExit("south", room2);
        // theater.setExits(null, null, null, outside);
        // pub.setExits(null, outside, null, null);
        // lab.setExits(outside, office, null, null);
        // office.setExits(null, null, null, lab);

        currentRoom = outside;  // start game outside
    }

    /**
     *  Main play routine.  Loops until end of play.
     */
    public void play() 
    {            
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.
                
        boolean finished = false;
        while (! finished) {
            Command command = parser.getCommand();
            finished = processCommand(command);
        }
        System.out.println("任务结束。");
    }

    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
        System.out.println();
        System.out.println("你是一名反恐精英，");
        System.out.println("你和你的小队来到炼狱小镇执行任务，");
        System.out.println("却被狡猾的匪徒埋伏，");
        System.out.println("你的队友全灭了，只剩下你一人，");
        System.out.println("而匪徒们已经埋下了炸弹要炸毁你们的重要物资。");
        System.out.println("想到你死去的队友，");
        System.out.println("你决定只身一人去拆除炸弹");
        System.out.println("挫败匪徒的阴谋，为你的兄弟们报仇！");
        System.out.println("首先，你要先找到拆弹器");
        System.out.println();
        System.out.println("你现在位于 " + currentRoom.getDescription());
        currentRoom.printExits();
    }

    /**
     * Given a command, process (that is: execute) the command.
     * @param command The command to be processed.
     * @return true If the command ends the game, false otherwise.
     */
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;
        overtime--;
        if(command.isUnknown()) {
            System.out.println("不可名状");
            return false;
        }

        Word commandWord = command.getCommandWord();
        // if (commandWord.equals("help")) {
            // printHelp();
        // }
        // else if (commandWord.equals("go")) {
            // goRoom(command);
        // }
        // else if (commandWord.equals("quit")) {
            // wantToQuit = quit(command);
        // }
        // else if (commandWord.equals("look")) {
            // lookRoom();
        // }
        // else if (commandWord.equals("check")) {
            // player.checkBag();
        // }
        // else if (commandWord.equals("pick")) {
            // pickThing();
        // }
        // else if (commandWord.equals("eat")) {
            // eatThing(command);
        // }
        // else if (commandWord.equals("Hp")) {
            // ;
        // }
        switch(commandWord){
            case HELP:
                printHelp();
                break;
            case GO:
                goRoom(command);
                break;
            case QUIT:
                wantToQuit = quit(command);
                break;
            case PICK:
                pickThing();
                break;
            case EAT:
                eatThing(command);
                break;
            case LOOK:
                lookRoom();
                break;
            case CHECK:
                player.checkBag();
                break;
            case DEFUSE:
                defuse();
                break;
        }
        System.out.println("剩余时间："+overtime);
        if(overtime <= 0 || gameover)
        {
            wantToQuit = true;
        }
        return wantToQuit;
    }

    // implementations of user commands:

    /**
     * Print out some help information.
     * Here we print some stupid, cryptic message and a list of the 
     * command words.
     */
    private void printHelp() 
    {
        System.out.println("你不知道要去哪里");
        System.out.println("你很迷茫");
        System.out.println();
        System.out.println("你可以做出的行动：");
        System.out.println("   go quit help look check pick eat defuse");
    }
    /** 
     * Try to go in one direction. If there is an exit, enter
     * the new room, otherwise print an error message.
     */
    private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("前往何处");
            return;
        }

        String direction = command.getSecondWord();

        // Try to leave current room.
        Room nextRoom = currentRoom.goNext(direction);

        if (nextRoom == null) {
            System.out.println("锁住了");
        }
        else {
            currentRoom = nextRoom;
            System.out.println("你现在位于" + currentRoom.getDescription());
            currentRoom.printExits();
        }
    }
    private void lookRoom()
    {
        if(currentRoom.getThing()==null)
            System.out.println("什么都没有");
        else System.out.println("有"+currentRoom.getThing().getName());
    }
    private void pickThing()
    {
        player.pick(currentRoom.getThing());
        if(currentRoom.getThing().getName()=="拆弹器")
        {havetirebal = true;}
        currentRoom.remove();
    }
    private void eatThing(Command command)
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("使用什么");
            return;
        }
        player.eat(command.getSecondWord());
    }
    private boolean defuse()
    {
        if(havetirebal == false)
        {
            System.out.println("你没有拆弹器");
            return gameover = false;
        }
       else{
        if(overtime>3 && haveBoom())
        {
            System.out.println("你拆除了炸弹");
            return gameover = true;
        }
        if(overtime<3)
        {
            System.out.println("你没有拆除炸弹，它爆炸了");
            return gameover = true;
        }
        if(!haveBoom())
        {
            System.out.println("这里没有炸弹");
            return gameover = false;
        }
        else
        {
            return gameover = false;
        }
    }
    }
    private boolean haveBoom()
    {
        if(currentRoom.getThing()==null)
        {
            return false;
        }
        else{
            if(currentRoom.getThing().getName()=="炸弹")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    /** 
     * "Quit" was entered. Check the rest of the command to see
     * whether we really quit the game.
     * @return true, if this command quits the game, false otherwise.
     */
    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("退出干嘛捏?");
            return false;
        }
        else {
            return true;  // signal that we want to quit
        }
    }
}